package l2ft.gameserver.network.l2.s2c;

import java.util.List;

import l2ft.commons.collections.CollectionUtils;
import l2ft.gameserver.dao.MailDAO;
import l2ft.gameserver.model.Player;
import l2ft.gameserver.model.mail.Mail;
import l2ft.gameserver.network.l2.c2s.RequestExDeleteSentPost;
import l2ft.gameserver.network.l2.c2s.RequestExRequestSentPost;
import l2ft.gameserver.network.l2.c2s.RequestExRequestSentPostList;


/**
 * ?????????? ??? ??????? ?? ?????? "sent mail", ????????? ??????
 * ????? ?? {@link RequestExRequestSentPostList}
 * ??? ??????? ?? ?????? ? ?????? ?????? {@link RequestExRequestSentPost}, ? ? ????? {@link ExReplySentPost}.
 * ??? ??????? ?? "delete" ?????? {@link RequestExDeleteSentPost}.
 * @see ExShowReceivedPostList ??????????? ?????? ???????? ?????
 */
public class ExShowSentPostList extends L2GameServerPacket
{
	private final List<Mail> mails;

	public ExShowSentPostList(Player cha)
	{
		mails = MailDAO.getInstance().getSentMailByOwnerId(cha.getObjectId());
		CollectionUtils.eqSort(mails);
	}

	// d dx[dSSddddd]
	@Override
	protected void writeImpl()
	{
		writeEx(0xAC);
		writeD((int)(System.currentTimeMillis() / 1000L));
		writeD(mails.size()); // ?????????? ?????
		for(Mail mail : mails)
		{
			writeD(mail.getMessageId()); // ?????????? id ??????
			writeS(mail.getTopic()); // ?????
			writeS(mail.getReceiverName()); // ??????????
			writeD(mail.isPayOnDelivery() ? 1 : 0); // ???? ??? 1 ?? ?????? ??????? ??????
			writeD(mail.getExpireTime()); // ????? ???????????????? ??????
			writeD(mail.isUnread() ? 1 : 0); // ?
			writeD(mail.getType() == Mail.SenderType.NORMAL ? 0 : 1); // returnable
			writeD(mail.getAttachments().isEmpty() ? 0 : 1); // 1 - ?????? ? ???????????, 0 - ?????? ??????
		}
	}
}